package com.campusexpresspickup.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.campusexpresspickup.dto.LogisticsCourierParam;
import com.campusexpresspickup.entity.LogisticsCourier;
import com.campusexpresspickup.exception.Asserts;
import com.campusexpresspickup.service.ILogisticsCourierService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.campusexpresspickup.common.RestResponse;
import com.campusexpresspickup.page.LogisticsCourierPage;
import java.util.Date;
import java.util.List;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 快递员表 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/logistics-courier")
public class LogisticsCourierController {

  private final ILogisticsCourierService logisticsCourierService;
  private final PasswordEncoder passwordEncoder;

  public LogisticsCourierController(
      ILogisticsCourierService logisticsCourierService,
      PasswordEncoder passwordEncoder) {
    this.logisticsCourierService = logisticsCourierService;
    this.passwordEncoder = passwordEncoder;
  }

  @Transactional
  @PostMapping(value = "/create", name = "添加")
  public Object create(@RequestBody LogisticsCourierParam logisticsCourierParam) {
    if (!StringUtils.hasText(StringUtils.trimAllWhitespace(logisticsCourierParam.getUserName()))) {
      Asserts.error("用户名不能为空");
    }
    if (!StringUtils.hasText(StringUtils.trimAllWhitespace(logisticsCourierParam.getPassword()))) {
      Asserts.error("密码不能为空");
    }
    int count;
    LogisticsCourier logisticsCourier = new LogisticsCourier();
    logisticsCourier.setUserName(logisticsCourierParam.getUserName());
    logisticsCourier.setPhone(logisticsCourierParam.getPhone());
    logisticsCourier.setGender(logisticsCourierParam.getGender());
    logisticsCourier.setPassword(passwordEncoder.encode(logisticsCourierParam.getPassword()));
    logisticsCourier.setCreateTime(new Date());
    if (logisticsCourier.getId() != null) {
      count = logisticsCourierService.getBaseMapper().updateById(logisticsCourier);
    } else {
      count = logisticsCourierService.getBaseMapper().insert(logisticsCourier);
    }
    if (count > 0) {
      return RestResponse.ok(logisticsCourier.getId(), "添加成功");
    }
    return RestResponse.ok(logisticsCourier.getId(), "添加失败");
  }

  @GetMapping(value = "/{courierId}", name = "详情")
  public Object getCourierById(@PathVariable Integer courierId) {
    LogisticsCourier logisticsCourier = new LogisticsCourier();
    logisticsCourier.setCourierId(courierId);
    LogisticsCourier courier = logisticsCourierService.getBaseMapper()
        .selectOne(new QueryWrapper<>(logisticsCourier));
    if (courier == null) {
      Asserts.error("快递员不存在");
    }
    return RestResponse.ok(courier, "查询成功");
  }

  @PostMapping(value = "/delete/{id}", name = "删除")
  public Object deleteCourierById(@PathVariable Long id) {
    return RestResponse.ok(logisticsCourierService.getBaseMapper().deleteById(id), "删除成功");
  }

  @PostMapping(value = "/list", name = "列表")
  public Object getCourierList(@RequestBody LogisticsCourierPage logisticsCourierPage) {
    PageHelper.startPage(logisticsCourierPage.getPageStart(), logisticsCourierPage.getPageSize());
    List<LogisticsCourier> logisticsCourierList = logisticsCourierService
        .getLogisticsCourierList(logisticsCourierPage);
    return RestResponse.ok(new PageInfo<>(logisticsCourierList), "查询成功");
  }
}
